<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	二维码管理（对接公众平台生成的带参数二维码）
	
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$return['params']=$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['mid'])){
		$params['mid']=trim($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(!empty($_RQ['storeid'])){
		$params['storeid']=trim($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['type'])){
		$params['type']=trim($_RQ['type']);
		$return['params']['type']=$_RQ['type'];
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_ewmup_list',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		if($item['qrtype']==1){
			$item['ewmurl']=$item['linkurl'];
		}elseif($item['qrtype']==3){
			$item['ewmurl']=custom_url('core/home.qr//id='.$item['id'],true);
		}else{
			$item['ewmurl']=$item['url'];
		}
		$item['member']=cfc('core')->member_show($item['mid']);
		$item['wxappewm']=tomedia($item['wxappewm']);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'二维码列表'),
			array('op'=>'edit','title'=>'创建二维码'),
		),
		'list'=>array('data'=>'list','tip'=>'二维码总数：'.$total,'list'=>array(
			array('name'=>'title','title'=>'二维码名称','type'=>'string'),
			array('name'=>'type','title'=>'二维码类型','type'=>'radio','options'=>'types'),
			array('name'=>'storeid','title'=>'所属门店','type'=>'select','options'=>'stores'),
			array('name'=>'member','title'=>'分销人','type'=>'member'),
			//array('name'=>'num','title'=>'被扫描次数','type'=>'string'),
			array('name'=>'linkurl','title'=>'H5二维码','type'=>'ewm'),
			array('name'=>'wxappewm','title'=>'小程序二维码','type'=>'image'),
			array('name'=>'createtime','title'=>'生成时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'upwxappewm','title'=>'刷新小程序二维码'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'二维码名称','type'=>'string'),
			array('name'=>'type','width'=>15,'title'=>'二维码类型','type'=>'radio','options'=>'types'),
			array('name'=>'title','width'=>15,'title'=>'二维码名称','type'=>'string'),
			array('type'=>'choose','width'=>10,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择分销人'),
		),
	);
	$return['stores']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['types']=$this->types;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_ewmup_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array('type'=>'0');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'添加二维码',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'回到二维码列表'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'二维码名称','type'=>'string'),
			array('name'=>'type','title'=>'二维码类型','type'=>'radio','options'=>'types'),
			array('name'=>'wxappid','title'=>'所属小程序','type'=>'select','options'=>'wxapps'),
			array('name'=>'storeid','title'=>'所属门店','type'=>'select','options'=>'stores','show'=>array('type'=>1)),
			array('type'=>'choose','action'=>'core/member.get_member/','name'=>'mid','title'=>'选择分销人','show'=>array('type'=>2)),
			array('name'=>'linkurl','title'=>'二维码链接','type'=>'string','show'=>array('type'=>0)),
			array('name'=>'wxappurl','title'=>'小程序链接','type'=>'string','show'=>array('type'=>0),'help'=>'例如：/jy_weishop/index/index'),
			
		),
	);
	$return['stores']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['wxapps']=pdo_getall('core_account_wxapp',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['types']=$this->types;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$preg = "/^http(s)?:\\/\\/.+/";
	if($postdata['type']==0 && !(preg_match($preg,$postdata['linkurl']))){
		exi('二维码URL:必须是https://或http://开头。','error');	
	}
	if($postdata['type']==1 && !$postdata['storeid']){
		exi('请选择门店','error');
	}
	if($postdata['type']==2 && !$postdata['mid']){
		exi('请选择分销人','error');
	}
	if($postdata['type']==1){
		$postdata['linkurl']=custom_url('jy_weishop/store.detail//issale=1&id='.$postdata['storeid'],true);
		$postdata['wxappurl']='/jy_weishop/store/detail/index?issale=1&id='.$postdata['storeid'];
		$pid='jy_ewmup1'.$postdata['storeid'];
	}elseif($postdata['type']==2){
		$postdata['linkurl']=custom_url('jy_weishop/index//fromuser='.$postdata['mid'],true);
		$postdata['wxappurl']='/jy_weishop/index/index?fromuser='.$postdata['mid'];
		$pid='jy_ewmup2'.$postdata['mid'];
	}else{
		$pid='jy_ewmup0'.$postdata['id'];
	}
	if($postdata['wxappid']){
		$appcode = model('jy_weishop/model')->get_wxappewm($postdata['wxappurl'],$pid,$postdata['wxappid']);
	}
	$id=intval($postdata['id']);
	$data=array(
		'title'=>trim($postdata['title']),
		'wxappid'=>trim($postdata['wxappid']),
		'type'=>intval($postdata['type']),
		'mid'=>intval($postdata['mid']),
		'storeid'=>intval($postdata['storeid']),
		'linkurl'=>trim($postdata['linkurl']),
		'wxappurl'=>trim($postdata['wxappurl']),
		'wxappewm'=>trim($appcode),
		'status'=>intval($postdata['status']),
	);
	if($id){
		pdo_update('jy_ewmup_list',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_ewmup_list',$data);
	}
	exi('操作成功！','','/qr/list/');
}
if($_RQ['op']=='look'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要查看的二维码','error');
	}
	$item=pdo_get('jy_ewmup_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('title','file'));
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'查看二维码',
		'tabs'=>array(
			array('op'=>'list','title'=>'回到二维码列表'),
		),
		'look'=>array(
			array('type'=>'string','name'=>'title','title'=>'二维码名称'),
			array('type'=>'image','name'=>'file','title'=>'二维码'),
		),
	);
	exi($return);
}
if($_RQ['op']=='upwxappewm'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_ewmup_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($item['wxappid']){
		if($item['type']==1){
			$pid='jy_ewmup1'.$item['storeid'];
		}elseif($item['type']==2){
			$pid='jy_ewmup2'.$item['mid'];
		}else{
			$pid='jy_ewmup0'.$item['id'];
		}
		$appcode = model('jy_weishop/model')->get_wxappewm($item['wxappurl'],$pid,$item['wxappid']);
		if($appcode){
			pdo_update('jy_ewmup_list',array('wxappewm'=>trim($appcode)),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}else{
		exi('没有绑定小程序，不能生成小程序二维码！');
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_ewmup_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_ewmup_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}